(ns demo.core)
(import java.time.LocalDate)
(import java.time.LocalDateTime)
(import java.time.LocalTime)
(import java.text.SimpleDateFormat)
(import java.time.format.DateTimeFormatter)

(defn get-route [activity applicant]
  (let [tem (str activity "@" applicant)]
    (mod (Math/abs (.hashCode tem)) 512)))


(defn get-label-route [applicant]
  (str "gold_pocket_goods" (get-route 777618 applicant)) )

(defn get-sku-index [sku]
  (mod sku 4096))

;;(defn parse-int [s] (Integer/parseInt (re-find #"\A-?\d+" s)))

(defn parse-long [s] (Long/valueOf  s))

(defn get-redis-cmd [activity applicant sku]
  (str "sismember um_mark_sku_set_" activity "_" (get-sku-index (parse-long sku)) " " sku "_" applicant "_" 1 ))

(defn redis-cmd [applicant sku]
  (get-redis-cmd 777618 applicant sku))

;; select * from gold_pocket_goods1 where activity_id=777618 and applicant='applicant' and goods_id=66284488818



(defn get-goods-sql
  ([activity applicant] (str "select * from gold_pocket_goods" (get-route activity applicant) " where activity_id=" activity " and applicant='" applicant "'" ) )
  ([activity applicant skuId] (str (get-goods-sql activity applicant) " and goods_id=" skuId) ))

(defn query-goods [activity applicant]
  (get-goods-sql activity applicant))


(defn get-today-ymd-str
  "获取 yyyy-MM-dd格式的当前日期字符串"
  []
  (.format (java.text.SimpleDateFormat. "yyyy-MM-dd") (new java.util.Date)))

(defn get-tomorrow-ymd-str
  "获取 yyyy-MM-dd格式的明天日期字符串"
  []
  (-> (LocalDate/now) (.plusDays 1) (.toString)))

(defn get-yesterday-ymd-str
  "获取 yyyy-MM-dd格式的昨天日期字符串"
  []
  (-> (LocalDate/now) (.plusDays -1) (.toString)))

(defn get-before-yesterday-ymd-str
  "获取 yyyy-MM-dd格式的昨天日期字符串"
  []
  (-> (LocalDate/now) (.plusDays -2) (.toString)))


(defn get-vc-id-sql [baseid]
  (str "SELECT * FROM vc_coupon_activity_sign WHERE id > "
       baseid
       " and create_time >'"
       (get-before-yesterday-ymd-str)
       " 23:59:59' and create_time <'"
       (get-today-ymd-str)
       " 00:00:00' AND yn=1 ORDER BY id ASC limit 6;"))

(defn get-vc-count-sql [line-split]
  (str "SELECT * FROM vc_coupon_activity_sign WHERE id =replaceid;"
       line-split
       "SELECT COUNT(1) FROM vc_coupon_activity_sign WHERE id > replaceid and create_time >'"
       (get-yesterday-ymd-str)
       " 00:00:00' and create_time <'"
       (get-today-ymd-str)
       " 00:00:00' AND yn=1;"))

(defn get-panhuo-count-sql [line-split]
  (str "select count(1) from gold_pocket_sku_info where status=0 and created > '"
       (get-yesterday-ymd-str)
       " 00:00:00' and created < '"
       (get-today-ymd-str)
       " 00:00:00';"
       line-split
       "select count(1) from gold_pocket_shop_info where status=0 and created > '"
       (get-yesterday-ymd-str)
       " 00:00:00' and created < '"
       (get-today-ymd-str)
       " 00:00:00';"))

(defn get-ech-applicant-count-sql [ applicant-type]
  (str "select count(1) from gold_pocket_applicant where status=0 and created >'"
         (get-yesterday-ymd-str)
         " 00:00:00' and created <'"
         (get-today-ymd-str)
         " 00:00:00' and applicant_type="
         applicant-type
         ";"))

(defn get-applicant-count-sql []
  (map get-ech-applicant-count-sql '(1 2 3 8)))

(defn get-activity-type-count-sql [line-split]
  (str "select activity_type, count(1) from gold_pocket_activity where status=0 and created >'"
       (get-yesterday-ymd-str)
       " 00:00:00' and created <'"
       (get-today-ymd-str)
       " 00:00:00' group by activity_type;"
       line-split
       "select activity_type, count(1) from gold_pocket_activity where activity_status=5 and modified>'"
       (get-yesterday-ymd-str)
       " 00:00:00' and modified <'"
       (get-today-ymd-str)
       " 00:00:00' group by activity_type;"))

(defn get-activity-count-list-sql [line-split]
  (str "select COUNT(id) from gold_pocket_activity where created >'"
       (get-yesterday-ymd-str)
       " 00:00:00' AND created <'"
       (get-today-ymd-str)
       " 00:00:00' ORDER BY created;"
       "\n"
       line-split
       "select * from gold_pocket_activity where created >'"
       (get-yesterday-ymd-str)
       " 00:00:00' AND created <'"
       (get-today-ymd-str)
       " 00:00:00' ORDER BY created;"))

(defn get-all-sql [baseid]
  (def line-split "<br/>")
  (str "vc查询最近的id:" line-split
       (get-vc-id-sql baseid)
       line-split
       "查询vc报名数量(注意替换id):" line-split
       (get-vc-count-sql line-split)
       line-split
       "查询盘货的sku和shop报名数量:" line-split
       (get-panhuo-count-sql line-split)
       line-split
       "查询报名人数量:" line-split
       (clojure.string/join "<br/>" (get-applicant-count-sql))
       line-split
       "查询活动创建和发布数量:" line-split
       (get-activity-type-count-sql line-split)
       line-split
       "查询活动列表:" line-split
       (get-activity-count-list-sql line-split)))

(defn get-local-date-time
  "获取 yyyy-MM-dd HH:mm:SS格式的明天日期"
  [year month day-of-month hour minute second]
  (LocalDateTime/of year month day-of-month hour minute second))

(defn get-next-day
  "获取下一天的日期"
  [local-date-time]
  (.plusDays local-date-time 1))

(defn plus-n-day
  [local-date-time n]
  (.plusDays local-date-time n))

(defn format-date-time
  "format date time to string"
  [local-date-time]
  (.format (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss") local-date-time) )


(defn get-calcu-date
  [local-date-time]
  (str (format-date-time local-date-time) 
       " "
       (format-date-time (get-next-day local-date-time)) 
       "<br/>"))

(defn get-all-calcu-date
  []
  (def target-date-time (get-local-date-time 2019 10 15 0 0 0))
  (map get-calcu-date (map #(plus-n-day target-date-time % )(range 30) ))
)


